﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NLite.Persistence;
using NUnit.Framework;
using NLite.NHPersistence;

namespace NLite.NHPersistenceTest
{
    [TestFixture]
    public class SecondLevelTest : TestBase
    {
        protected override void OnInit()
        {
            using (var tx = Session.BeginTransaction())
            {

                Guid id = Guid.Empty;
                for (int i = 0; i < 1; i++)
                {
                    UserInfo user = new UserInfo()
                    {
                        Name = "TestName" + ('A' + i % 32),
                        Age = i,
                        Books = new List<BookInfo>()
                    };

                    for (int j = 0; j < 3; j++)
                    {
                        user.Books.Add(new BookInfo() { User = user });
                    }
                    repository.SaveOrUpdate(user);
                    id = user.Id;
                }
            }
        }

        [Test]
        public void test_CacheQuery()
        {
            Session = base.Factory.OpenSession() as INhibernateSession;
            using (var tx = Session.BeginTransaction())
            {
                Factory.SessionFactory.Statistics.IsStatisticsEnabled = true;

                Stopwatch stopwatch = new Stopwatch();
                query = Session.Query<UserInfo>();

                stopwatch.Start();
                var list = (from u in query.Cacheable()//.CacheMode(CacheMode.Get).CacheRegion("hour")
                            select u).ToList();
                stopwatch.Stop();
                Console.WriteLine(stopwatch.ElapsedTicks);

                Console.WriteLine("Second:_________________________");

                for (int i = 0; i < 10; i++)
                {
                    Factory.SessionFactory.Statistics.Clear();

                    stopwatch.Reset();
                    var list2 = (from u in query.Cacheable()
                                 select u).ToList();
                    stopwatch.Stop();
                    Console.WriteLine(stopwatch.ElapsedTicks);
                }
            }
        }
    }
}
